<?php

/**
 * Description of CeoController
 *
 * @author Nylaye
 */
class CEO_RoutesController extends CEO_Controller
{

    public function init()
    {
        if (!Model3_Auth::isAuth())
        {
            $this->redirect('Index/index');
        }
    }

    public function sequentialActivityAction()
    {
        $startDate = new DateTime();
        $em = $this->getEntityManager('DefaultDb');
        $routes = $em->getRepository('DefaultDb_Entities_Route')->findAll();
        $criteria = array();
        $orderBy = array('routeDate');
        $date = new DateTime();
        $dateSecond = new DateTime();
        $dateSecond->modify('+1 day');
        $query = $em->getRepository('DefaultDb_Entities_SequentialActivities')->createQueryBuilder('s')
                ->select('DISTINCT s.routeDate')
                ->Where('s.shippingDate >= \'' . $date->format('Y-m-d') . '\'')
                ->andWhere('s.shippingDate <= \'' . $dateSecond->format('Y-m-d') . '\'')
                ->orderBy('s.shippingDate')
                ->getQuery();
        $routesHour = $query->getResult();
        $this->view->startDate = $startDate;
        $this->view->routes = $routes;
        $this->view->routesHour = $routesHour;
        // archivos
        $url = $this->view->url(array('action' => 'axSequentialActivity'));
        $urlGetOptionsHour = $this->view->url(array('module' => false, 'controller' => 'Ajax_Reports',
            'action' => 'getOptionsHour'));
        $this->view->getJsManager()->addJsVar('urlGetOptionsHour', json_encode($urlGetOptionsHour));
        $this->view->getJsManager()->addJsVar('urlGetSecuencialActivity', json_encode($url));
        $this->view->getJsManager()->addJs('jquery/jquery-ui-1.8.12.custom.min.js');
        $this->view->getJsManager()->addJs('application/Admin/Reports/secuencialActivity' . VERSION_JS . '.js');
        $this->view->setTemplate('Responsive-3.0');
        $this->view->title = 'CEO';
        $this->view->description = 'Panel de control';
    }
    
    public function axSequentialActivityAction()
    {
        $em = $this->getEntityManager('DefaultDb');
        $ordersRepository = $em->getRepository('DefaultDb_Entities_M3CommerceOrder');
        $routesRepository = $em->getRepository('DefaultDb_Entities_Route');
        $routesPointsRepository = $em->getRepository('DefaultDb_Entities_RoutePoint');
        $configRepository = $em->getRepository('DefaultDb_Entities_Configuration');
        $configurations = $configRepository->find(1);
        $minutesConfiguration = $configurations->getMinutesPerPoint();
        $registers = false;
        $routepointsByRoute = false;
        if($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            $starDate = new DateTime($post['starDate']);
            $route = $routesRepository->find($post['route']);
            $routepointsByRoute = $routesPointsRepository->findBy( array('status'=>1),array('order' => 'asc'));
            $hour = isset($post['hourRoute']) && $post['hourRoute'] ? $post['hourRoute']: null;
            $registers = $ordersRepository->getSecuencialActivityByDateByRoute($starDate,$route,null);
        }
        $this->view->startDate = isset($post['hourRoute']) && $post['hourRoute'] ? new DateTime($post['hourRoute']): $starDate;
        $this->view->minutesConfiguration = $minutesConfiguration;
        $this->view->routePointsByRoute = $routepointsByRoute;
        $this->view->registers = $registers;
        var_dump($registers);
        $this->view->setUseTemplate(false);
    }

}